namespace KPIS.GERP.Models.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class update0107 : DbMigration
    {
        public override void Up()
        {
            //RenameTable(name: "dbo.MessageSend", newName: "message_send");
            //RenameTable(name: "dbo.MessageReply", newName: "message_reply");
            DropForeignKey("dbo.SysMessage", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl");
            DropForeignKey("dbo.SysMessage", "AppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysMessage", "FromEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysMessage", "ToEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysApprovalTaskDtl", "CommandByEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysApprovalTaskDtl", "ApprovalTaskId", "dbo.SysApprovalTask");
            DropForeignKey("dbo.SysApprovalTaskDtl", "ExecPosGroupId", "dbo.sys_exec_pos_group");
            DropForeignKey("dbo.SysApprovalTaskDtl", "PosId", "dbo.hrm_mp_positions");
            DropForeignKey("dbo.SysApprovalTaskDtl", "AppPathId", "dbo.SysApprovePath");
            DropForeignKey("dbo.SysApprovalTaskDtlEmployee", "EmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysApprovalTaskDtlEmployee", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl");
            DropForeignKey("dbo.SysApprovalTask", "OriginalAppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysApprovalTask", "OriginalApplyToEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysApprovalTask", "CurrentAppPathId", "dbo.SysApprovePath");
            DropIndex("dbo.SysMessage", new[] { "ApprovalTaskDtlId" });
            DropIndex("dbo.SysMessage", new[] { "AppPrcId" });
            DropIndex("dbo.SysMessage", new[] { "FromEmpId" });
            DropIndex("dbo.SysMessage", new[] { "ToEmpId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "CommandByEmpId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "ApprovalTaskId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "ExecPosGroupId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "PosId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "AppPathId" });
            DropIndex("dbo.SysApprovalTaskDtlEmployee", new[] { "EmpId" });
            DropIndex("dbo.SysApprovalTaskDtlEmployee", new[] { "ApprovalTaskDtlId" });
            DropIndex("dbo.SysApprovalTask", new[] { "OriginalAppPrcId" });
            DropIndex("dbo.SysApprovalTask", new[] { "OriginalApplyToEmpId" });
            DropIndex("dbo.SysApprovalTask", new[] { "CurrentAppPathId" });
            //RenameColumn(table: "dbo.message_send", name: "SendId", newName: "send_seq");
            //RenameColumn(table: "dbo.message_send", name: "SendDate", newName: "send_date");
            //RenameColumn(table: "dbo.message_send", name: "RefId", newName: "ref_seq");
            //RenameColumn(table: "dbo.message_send", name: "RefType", newName: "ref_type");
            //RenameColumn(table: "dbo.message_send", name: "RefNo", newName: "ref_no");
            //RenameColumn(table: "dbo.message_send", name: "SendText", newName: "send_text");
            //RenameColumn(table: "dbo.message_send", name: "FromEmpId", newName: "from_emp_seq");
            //RenameColumn(table: "dbo.message_send", name: "ToEmpId", newName: "to_emp_seq");
            //RenameColumn(table: "dbo.message_send", name: "FromSendId", newName: "from_send_seq");
            //RenameColumn(table: "dbo.message_send", name: "MessageStatus", newName: "message_status");
            //RenameColumn(table: "dbo.message_send", name: "RecordStatus", newName: "record_status");
            //RenameColumn(table: "dbo.message_send", name: "CreatedBy", newName: "created_by");
            //RenameColumn(table: "dbo.message_send", name: "CreatedWhen", newName: "created_when");
            //RenameColumn(table: "dbo.message_send", name: "UpdatedBy", newName: "updated_by");
            //RenameColumn(table: "dbo.message_send", name: "UpdatedWhen", newName: "updated_when");
            //RenameColumn(table: "dbo.message_reply", name: "ReplyId", newName: "reply_seq");
            //RenameColumn(table: "dbo.message_reply", name: "ReplyDate", newName: "reply_date");
            //RenameColumn(table: "dbo.message_reply", name: "RefNo", newName: "ref_no");
            //RenameColumn(table: "dbo.message_reply", name: "ReplyType", newName: "reply_type");
            //RenameColumn(table: "dbo.message_reply", name: "ReplyText", newName: "reply_text");
            //RenameColumn(table: "dbo.message_reply", name: "FromEmpId", newName: "from_emp_seq");
            //RenameColumn(table: "dbo.message_reply", name: "ToEmpId", newName: "to_emp_seq");
            //RenameColumn(table: "dbo.message_reply", name: "FromSendId", newName: "from_send_seq");
            //RenameColumn(table: "dbo.message_reply", name: "MessageStatus", newName: "message_status");
            //RenameColumn(table: "dbo.message_reply", name: "RecordStatus", newName: "record_status");
            //RenameColumn(table: "dbo.message_reply", name: "CreatedBy", newName: "created_by");
            //RenameColumn(table: "dbo.message_reply", name: "CreatedWhen", newName: "created_when");
            //RenameColumn(table: "dbo.message_reply", name: "UpdatedBy", newName: "updated_by");
            //RenameColumn(table: "dbo.message_reply", name: "UpdatedWhen", newName: "updated_when");
            CreateTable(
                "dbo.SysOTPData",
                c => new
                    {
                        OTPDataId = c.Int(nullable: false, identity: true),
                        Key = c.String(),
                        Value = c.String(),
                        UniqueId = c.Guid(nullable: false),
                    })
                .PrimaryKey(t => t.OTPDataId)
                .ForeignKey("dbo.SysOTP", t => t.UniqueId, cascadeDelete: true)
                .Index(t => t.UniqueId);
            
            CreateTable(
                "dbo.SysServerStartUp",
                c => new
                    {
                        TaskId = c.Guid(nullable: false, identity: true),
                        System = c.String(),
                        DateInit = c.DateTime(nullable: false),
                        Title = c.String(),
                        Description = c.String(),
                    })
                .PrimaryKey(t => t.TaskId);
            
            //AddColumn("dbo.message_send", "approve_path_seq", c => c.Int());
            AlterColumn("dbo.SysOTP", "UniqueId", c => c.Guid(nullable: false, identity: true));
            //AlterColumn("dbo.message_send", "remarks", c => c.String());
            //AlterColumn("dbo.message_reply", "remarks", c => c.String());
            DropTable("dbo.SysMessage");
            DropTable("dbo.SysApprovalTaskDtl");
            DropTable("dbo.SysApprovalTaskDtlEmployee");
            DropTable("dbo.SysApprovalTask");
        }
        
        public override void Down()
        {
            CreateTable(
                "dbo.SysApprovalTask",
                c => new
                    {
                        ApprovalTaskId = c.Int(nullable: false, identity: true),
                        Code = c.String(),
                        StepNo = c.Int(nullable: false),
                        OriginalRequestTitle = c.String(),
                        RequestAmount = c.Decimal(precision: 18, scale: 2),
                        Reasons = c.String(),
                        ActionCount = c.Int(nullable: false),
                        CurrentAppPathId = c.Int(),
                        OriginalApplyToEmpId = c.Int(nullable: false),
                        OriginalAppPrcId = c.Int(nullable: false),
                        ApprovalTaskStatus = c.Int(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        RefId = c.Int(),
                        ConfirmAs = c.String(),
                        ApproveAs = c.String(),
                        Completed = c.Boolean(nullable: false),
                        CompletedActionBy = c.Int(),
                        ApprovedDate = c.DateTime(),
                        ApprovedBy = c.Int(),
                        CompletedActionWhen = c.DateTime(),
                        CompletedDtlId = c.Int(),
                        CompletedMessageId = c.Int(),
                        CompletedCommandAction = c.String(),
                    })
                .PrimaryKey(t => t.ApprovalTaskId);
            
            CreateTable(
                "dbo.SysApprovalTaskDtlEmployee",
                c => new
                    {
                        ApprovalTaskDtlEmpId = c.Int(nullable: false, identity: true),
                        ApprovalTaskDtlId = c.Int(),
                        EmpId = c.Int(),
                    })
                .PrimaryKey(t => t.ApprovalTaskDtlEmpId);
            
            CreateTable(
                "dbo.SysApprovalTaskDtl",
                c => new
                    {
                        ApprovalTaskDtlId = c.Int(nullable: false, identity: true),
                        CreatedOnActionCount = c.Int(nullable: false),
                        IsCancel = c.Boolean(nullable: false),
                        AmountMin = c.Decimal(precision: 18, scale: 2),
                        AmountMax = c.Decimal(precision: 18, scale: 2),
                        SystemRemarks = c.String(),
                        ApprovalTaskId = c.Int(),
                        AppPathId = c.Int(),
                        FromMessageId = c.Int(),
                        FromApprovalTaskDtlId = c.Int(),
                        IsApproveAble = c.Boolean(nullable: false),
                        IsForwardable = c.Boolean(nullable: false),
                        IsRejectable = c.Boolean(nullable: false),
                        IsPendable = c.Boolean(nullable: false),
                        IsSendBackable = c.Boolean(nullable: false),
                        IsLockable = c.Boolean(nullable: false),
                        IsTakebackAble = c.Boolean(nullable: false),
                        ApproverType = c.Int(nullable: false),
                        PosId = c.Int(),
                        ExecPosGroupId = c.Int(),
                        CommandByEmpId = c.Int(),
                        CommandAction = c.Int(),
                        TaskStatusBefore = c.Int(nullable: false),
                        TaskStatusAfter = c.Int(nullable: false),
                        StepBefore = c.Int(nullable: false),
                        StepAfter = c.Int(nullable: false),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ApprovalTaskDtlId);
            
            CreateTable(
                "dbo.SysMessage",
                c => new
                    {
                        MessageId = c.Int(nullable: false, identity: true),
                        Code = c.String(),
                        Date = c.DateTime(),
                        Title = c.String(),
                        Description = c.String(),
                        FromEmpId = c.Int(),
                        ToEmpId = c.Int(),
                        FromMessageId = c.Int(),
                        RuleApproveID = c.Int(),
                        Remarks = c.String(),
                        MessageStatus = c.Int(nullable: false),
                        ApprovalTaskDtlId = c.Int(),
                        AppPrcId = c.Int(),
                        ActionCount = c.Int(nullable: false),
                        RuleApprove = c.Int(nullable: false),
                        ApprovalTask = c.Int(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.MessageId);
            
            DropIndex("dbo.SysOTPData", new[] { "UniqueId" });
            DropForeignKey("dbo.SysOTPData", "UniqueId", "dbo.SysOTP");
            AlterColumn("dbo.message_reply", "Remarks", c => c.String());
            AlterColumn("dbo.message_send", "Remarks", c => c.String());
            AlterColumn("dbo.SysOTP", "UniqueId", c => c.Guid(nullable: false));
            DropColumn("dbo.message_send", "approve_path_seq");
            DropTable("dbo.SysServerStartUp");
            DropTable("dbo.SysOTPData");
            RenameColumn(table: "dbo.message_reply", name: "updated_when", newName: "UpdatedWhen");
            RenameColumn(table: "dbo.message_reply", name: "updated_by", newName: "UpdatedBy");
            RenameColumn(table: "dbo.message_reply", name: "created_when", newName: "CreatedWhen");
            RenameColumn(table: "dbo.message_reply", name: "created_by", newName: "CreatedBy");
            RenameColumn(table: "dbo.message_reply", name: "record_status", newName: "RecordStatus");
            RenameColumn(table: "dbo.message_reply", name: "message_status", newName: "MessageStatus");
            RenameColumn(table: "dbo.message_reply", name: "from_send_seq", newName: "FromSendId");
            RenameColumn(table: "dbo.message_reply", name: "to_emp_seq", newName: "ToEmpId");
            RenameColumn(table: "dbo.message_reply", name: "from_emp_seq", newName: "FromEmpId");
            RenameColumn(table: "dbo.message_reply", name: "reply_text", newName: "ReplyText");
            RenameColumn(table: "dbo.message_reply", name: "reply_type", newName: "ReplyType");
            RenameColumn(table: "dbo.message_reply", name: "ref_no", newName: "RefNo");
            RenameColumn(table: "dbo.message_reply", name: "reply_date", newName: "ReplyDate");
            RenameColumn(table: "dbo.message_reply", name: "reply_seq", newName: "ReplyId");
            RenameColumn(table: "dbo.message_send", name: "updated_when", newName: "UpdatedWhen");
            RenameColumn(table: "dbo.message_send", name: "updated_by", newName: "UpdatedBy");
            RenameColumn(table: "dbo.message_send", name: "created_when", newName: "CreatedWhen");
            RenameColumn(table: "dbo.message_send", name: "created_by", newName: "CreatedBy");
            RenameColumn(table: "dbo.message_send", name: "record_status", newName: "RecordStatus");
            RenameColumn(table: "dbo.message_send", name: "message_status", newName: "MessageStatus");
            RenameColumn(table: "dbo.message_send", name: "from_send_seq", newName: "FromSendId");
            RenameColumn(table: "dbo.message_send", name: "to_emp_seq", newName: "ToEmpId");
            RenameColumn(table: "dbo.message_send", name: "from_emp_seq", newName: "FromEmpId");
            RenameColumn(table: "dbo.message_send", name: "send_text", newName: "SendText");
            RenameColumn(table: "dbo.message_send", name: "ref_no", newName: "RefNo");
            RenameColumn(table: "dbo.message_send", name: "ref_type", newName: "RefType");
            RenameColumn(table: "dbo.message_send", name: "ref_seq", newName: "RefId");
            RenameColumn(table: "dbo.message_send", name: "send_date", newName: "SendDate");
            RenameColumn(table: "dbo.message_send", name: "send_seq", newName: "SendId");
            CreateIndex("dbo.SysApprovalTask", "CurrentAppPathId");
            CreateIndex("dbo.SysApprovalTask", "OriginalApplyToEmpId");
            CreateIndex("dbo.SysApprovalTask", "OriginalAppPrcId");
            CreateIndex("dbo.SysApprovalTaskDtlEmployee", "ApprovalTaskDtlId");
            CreateIndex("dbo.SysApprovalTaskDtlEmployee", "EmpId");
            CreateIndex("dbo.SysApprovalTaskDtl", "AppPathId");
            CreateIndex("dbo.SysApprovalTaskDtl", "PosId");
            CreateIndex("dbo.SysApprovalTaskDtl", "ExecPosGroupId");
            CreateIndex("dbo.SysApprovalTaskDtl", "ApprovalTaskId");
            CreateIndex("dbo.SysApprovalTaskDtl", "CommandByEmpId");
            CreateIndex("dbo.SysMessage", "ToEmpId");
            CreateIndex("dbo.SysMessage", "FromEmpId");
            CreateIndex("dbo.SysMessage", "AppPrcId");
            CreateIndex("dbo.SysMessage", "ApprovalTaskDtlId");
            AddForeignKey("dbo.SysApprovalTask", "CurrentAppPathId", "dbo.SysApprovePath", "AppPathId");
            AddForeignKey("dbo.SysApprovalTask", "OriginalApplyToEmpId", "dbo.hrm_ps_employees", "emp_seq", cascadeDelete: true);
            AddForeignKey("dbo.SysApprovalTask", "OriginalAppPrcId", "dbo.SysApproveProcess", "AppPrcId", cascadeDelete: true);
            AddForeignKey("dbo.SysApprovalTaskDtlEmployee", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl", "ApprovalTaskDtlId");
            AddForeignKey("dbo.SysApprovalTaskDtlEmployee", "EmpId", "dbo.hrm_ps_employees", "emp_seq");
            AddForeignKey("dbo.SysApprovalTaskDtl", "AppPathId", "dbo.SysApprovePath", "AppPathId");
            AddForeignKey("dbo.SysApprovalTaskDtl", "PosId", "dbo.hrm_mp_positions", "pos_seq");
            AddForeignKey("dbo.SysApprovalTaskDtl", "ExecPosGroupId", "dbo.sys_exec_pos_group", "exec_pos_group_seq");
            AddForeignKey("dbo.SysApprovalTaskDtl", "ApprovalTaskId", "dbo.SysApprovalTask", "ApprovalTaskId");
            AddForeignKey("dbo.SysApprovalTaskDtl", "CommandByEmpId", "dbo.hrm_ps_employees", "emp_seq");
            AddForeignKey("dbo.SysMessage", "ToEmpId", "dbo.hrm_ps_employees", "emp_seq");
            AddForeignKey("dbo.SysMessage", "FromEmpId", "dbo.hrm_ps_employees", "emp_seq");
            AddForeignKey("dbo.SysMessage", "AppPrcId", "dbo.SysApproveProcess", "AppPrcId");
            AddForeignKey("dbo.SysMessage", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl", "ApprovalTaskDtlId");
            RenameTable(name: "dbo.message_reply", newName: "MessageReply");
            RenameTable(name: "dbo.message_send", newName: "MessageSend");
        }
    }
}
